package com.xcfw.dorm.controller.dormassistant;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.*;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.seviceimpl.dormassistant.ExamineServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @program: Dorm
 * @description: 查寝
 * @author: Bruse Queen
 * @create: 2018-04-07 16:42
 **/
@RestController
@Slf4j
@RequestMapping(value = "/dormAssistant")
@CrossOrigin
public class ExamineController extends BaseController {
    @Autowired
    @Qualifier("examineServiceImpl")
    private ZSGCService zsgcService;

    @RequestMapping(value = "/addExamine", method = RequestMethod.POST)
    public ReturnJson addExamine(@RequestBody Examine examine) {
        try {
            zsgcService.add(examine);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝信息新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineController---addExamine--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteExamine", method = RequestMethod.POST)
    public ReturnJson deleteExamine(@RequestBody Examine examine) {
        try {
            zsgcService.delete(examine.getId());
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝信息删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineController---deleteExamine--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateExamine", method = RequestMethod.POST)
    public ReturnJson updateExamine(@RequestBody Examine examine) {
        try {
            zsgcService.update(examine);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝信息修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineController---updateExamine--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectExamine", method = RequestMethod.POST)
    public ReturnJson selectExamine(@RequestBody Examine examine) {
        try {
            result = (ReturnJson) zsgcService.select(examine);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineController---selectExamine--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
